function worldMapLoaded(map, admin, activeBusstopId){
	populateMapWithBusstops(admin, activeBusstopId);

	$("#mapLoading").hide();
	$("#map").show();
	var worldMap = $("#map").mapbox({
		mousewheel: false,
	}); 

	$("#mapBusstopsLoading").show();


}


function addBusstopInMapEvent(busstopIcon){

	$(busstopIcon).draggable({
		zIndex: 2700,
		start: function(){
			$("#map").mapbox("toggleDragging");
			$(busstopIcon).css('background-color','rgba(240,240,0,0.5)');
		},
		drag: function(){

		},
		stop: function(){
			$("#map").mapbox("toggleDragging");
			$.ajax({  
			  type: "POST", 
			  data: "y="+$(busstopIcon).position().top+"&x="+$(busstopIcon).position().left+"&id="+$(busstopIcon).children("#busstopId").html(),
			  url: "/fetch/busstopsUpdate", 
			  complete: function(data) { 
				  if($.trim(data.responseText) == "1"){
					$(busstopIcon).css('background-color','rgba(0,240,0,0.5)');
				  }else{
  					$(busstopIcon).css('background-color','rgba(240,0,0,0.5)');
					alert("ajax error:"+data.responseText);
				  }
			  },
			});
		}
	});

}

function summonBusStop(id, name, busstopX, busstopY, buslines, editable, activeBusstopId){

	var lastLines;
	var busstopIcon;

	 $("#mapInner").append(
            busstopIcon = $('<div class="busstopIcon"><div style="display:none" id="busstopId">'+id+'</div></div>')
                .css('position', 'absolute')
                .css('top', busstopY + 'px')
                .css('left', busstopX + 'px')
				.css('z-index', '1000')
				.append('<div class="busstopName">'+name+'</div>')
				.append(lastLines = $('<div class="busstopLines"></div>'))
        );

	 if(editable){
		addBusstopInMapEvent(busstopIcon);
		
	 }

	 if(id == activeBusstopId){
		$("#map").mapbox("center", { x:busstopX, y:busstopY });
		$(busstopIcon).css('background-color','rgba(140,140,250,0.5)');
	 }

	 buslines.each(
		 function(){
			lastLines.append(
				$('<div class="oneBusstopLine">'+($(this).children("name")).text()+'</div>')
					.css('background-color', 'rgb('+($(this).children("redValue")).text()+','+($(this).children("greenValue")).text()+','+($(this).children("blueValue")).text()+')')
			);
		}
	);
	$("#mapInner").find("div.busstopLines").append('<div style="clear:both;"></div>');
}

function populateMapWithBusstops(admin, activeBusstopId){
	var busStopsXML;
	$.ajax({  
      type: "GET",  
      url: "/fetch/busstops", 
      complete: function(data) {  
		  $("#mapIcons").css('position','absolute')
						.css('left', $("#map").position().left)
						.css('top', $("#map").position().top);
		  busStopsXML = $.parseXML( data.responseText );
		  $busStopsXML = $(busStopsXML);
		  $busStopsXML.find("busstop").each(function()
		  {
				var id = $(this).children("id").text(); 
				var name = $(this).children("name").text();
				var x = $(this).children("xCoordinate").text();
				var y = $(this).children("yCoordinate").text();
				// var buslines = $(this).find("yCoordinate").text();
				var buslinesXML = $(this).children("buslines");
				var buslines = $(buslinesXML).children("busline");


			  summonBusStop(id, name, x, y, buslines, admin, activeBusstopId);
			  //alert("Peatus nimega: "+);
		  });
		  $("#mapBusstopsLoading").hide();
		  return true;
      }
    });
	
 


}

function enlargeWorldMap(){
	$('#mapContainer').animate({
		width: '1150px',
		height: '600px'
	}, 500, function() {
		$("#map").mapbox({mousewheel: false}); 
	// Animation complete.
	});
}
function ensmallWorldMap(){
	$('#mapContainer').animate({
		width: '700px',
		height: '400px'
	}, 500, function() {
		$("#map").mapbox({mousewheel: false}); 
	// Animation complete.
	});
}